// import { validate } from "schema-utils";

// // schema for options object
// const schema = {
//   type: "object",
//   properties: {
//     test: {
//       type: "string",
//     },
//   },
// };
const HtmlWebpackPlugin = require("html-webpack-plugin");
class SetScriptTimestampPlugin {
  constructor(options) {
    this.options = options;
  }
  apply(compiler) {
    compiler.hooks.compilation.tap("MyPlugin", (compilation) => {
      console.log("The compiler is starting a new compilation...");

      // Static Plugin interface |compilation |HOOK NAME | register listener
      HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
        "MyPlugin", // <-- Set a meaningful name here for stacktraces
        (htmlPluginData, cb) => {
          // Manipulate the content
          console.log(htmlPluginData, this.options);
          // let jsScr = htmlPluginData.assets.js[0];
          // htmlPluginData.assets.js = [];
          // let result = `
          //       <script>
          //           let scriptDOM = document.createElement("script");
          //           let jsScr = "./${jsScr}";
          //           scriptDOM.src = jsScr + "?" + new Date().getTime();
          //           document.body.appendChild(scriptDOM)
          //       </script>
          //   `;
          let resultHTML = htmlPluginData.html.replace(
            "<!--title-->",
            this.options.title || "document"
          );
          // // 返回修改后的结果
          htmlPluginData.html = resultHTML;
          // data.html += 'The Magic Footer'
          // Tell webpack to move on
          cb(null, htmlPluginData);
        }
      );
    });
  }
}
module.exports = SetScriptTimestampPlugin;
// 执行 compilation.plugin 方法，并传入两个参数：插件事件和回调方法。
// 所谓“插件事件”即插件所提供的一些事件，用于监听插件状态，这里列举几个 html-webpack-plugin 提供的事件（完整可查看《html-webpack-plugin》）：
// Async:

// html-webpack-plugin-before-html-generation
// html-webpack-plugin-before-html-processing
// html-webpack-plugin-alter-asset-tags
// Sync:

// html-webpack-plugin-alter-chunks
